<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.description" placeholder="图片信息" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />

      <el-select v-model="listQuery.categoryId" placeholder="图片分类" clearable class="filter-item" style="width: 110px">
        <el-option :key="item.id" :value="item.id" v-for="item in category" :label="item.name" />
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" style="float: right;margin-right: 10px;" type="primary" icon="el-icon-rank" @click="addOrUpdateHandle()">新增</el-button>
    </div>

    <el-table v-loading="listLoading" :default-sort = "{prop: 'id', order: 'descending'}" :data="list" border fit highlight-current-row style="width: 100%" @sort-change="sortChange">
      <el-table-column align="center" label="图片ID" prop="id" width="100" sortable/>

      <el-table-column align="center" label="图片信息" min-width="100" prop="description"/>

      <el-table-column align="center" label="图片" width="400">
        <template slot-scope="scope">
          <el-popover placement="left" trigger="hover">
            <img :src="scope.row.image" width="300">
            <img slot="reference" :src="scope.row.image" style="width: 40%">
          </el-popover>
        </template>
      </el-table-column>

      <el-table-column align="center" label="分类" width="100">
        <template slot-scope="scope">
          <el-tag >
            {{ scope.row.category.name }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column :formatter="formatTime" width="102" align="center" sortable="custom" label="创建时间" prop="createTime"/>

      <el-table-column :formatter="formatTime" width="102" align="center" sortable="custom" label="修改时间" prop="updateTime"/>

      <el-table-column width="200" align="center" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" icon="el-icon-edit" type="primary" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button size="mini" icon="el-icon-delete" type="danger" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.current" :limit.sync="listQuery.size" @pagination="getList" />
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getList"/>
  </div>


</template>

<script>
import AddOrUpdate from './add-or-update'
import { putUser } from '@/api/user'
import { getPictureList, deletePictureById, getAllCategory } from '@/api/picture'
import Pagination from '@/components/Pagination'
import { parseTime } from '@/utils'
export default {
  name: 'Picture',
  components: { AddOrUpdate, Pagination },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      srcList:[],
      addOrUpdateVisible: false,
      category: [],
      listQuery: {
        current: 1,
        size: 10,
        ascs: undefined,
        descs: undefined,
      }
    }
  },

  // created() {
  //   this.getList()
  // },
  methods: {
    getList() {
      this.listLoading = true
      getPictureList(this.listQuery).then(response => {
        if (response.data) {
          this.list = response.data.records
          this.total = response.data.total
        }
        this.listLoading = false
      })
    },
    // 排序
    sortChange(data) {
      if (data.order === 'ascending') {
        this.listQuery.descs = undefined
        this.listQuery.ascs = data.prop.replace(/([A-Z])/g, '_$1').toLowerCase()
      } else {
        this.listQuery.ascs = undefined
        this.listQuery.descs = data.prop.replace(/([A-Z])/g, '_$1').toLowerCase()
      }
      this.handleFilter()
    },
    // 搜索
    handleFilter() {
      this.listQuery.current = 1
      this.getList()
    },
    // 格式化时间
    formatTime(row, column, cellValue) {
      return parseTime(cellValue)
    },
    // 禁用/启用用户
    updateUserStatus(id, isDisable) {
      this.$confirm('确定要' + (isDisable ? '禁用' : '启用') + '该用户么?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        putUser({ id, isDisable }).then(response => {
          this.$message.success(response.msg)
          this.getList()
        })
      })
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    //删除
    deleteHandle(id){
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletePictureById(id).then(response => {
          this.$message.success(response.msg)
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    //分类全部
    getAllCategory(){
      getAllCategory().then(res=>{
          console.log("==============")
          this.category = res.data
          console.log(this.category)
      }).catch(err=>{
        console.log(err)
      })
    }
  },
  mounted(){
    this.getAllCategory()
  }
}
</script>

<style scoped>
.filter-container {
    padding-bottom: 20px;
}
</style>
